﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MSH.Common.Classes;
using WP7Client.Helpers;
using WP7Client.ServerManager;

namespace WP7Client.Controller
{
    public class RegistrationController : BaseController
    {
        #region Members

        private String _Username;
        private String _Password;
        private String _Password2;
        private String _MobilePhoneNumber;

        #endregion

        #region Get&Set

        public ICommand RegisterCommand 
        { 
            get; 
            internal set; 
        }

        public String Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        public String Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public String Password2
        {
            get { return _Password2; }
            set { _Password2 = value; }
        }

        public String MobilePhoneNumber
        {
            get { return _MobilePhoneNumber; }
            set { _MobilePhoneNumber = value; }
        }

        #endregion

        public RegistrationController()
        {
            RegisterCommand = new RelayCommand(RegisterExecute);
        }

        /// <summary>
        /// Registreer een nieuwe gebruiker
        /// </summary>
        private void RegisterExecute()
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(Password2) || String.IsNullOrEmpty(MobilePhoneNumber))
            {
                GlobalHelper.ShowMessage("Error", "All fields need to be filled.");
                return;
            } else if(!Password.Equals(Password2))
            {
                GlobalHelper.ShowMessage("Error", "Password confirmation doesn't match your new password.");
                return;
            }

            Loading = true;
            HttpGet registerHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "Register/{0}/{1}/{2}", Username, GlobalHelper.MD5(Password), MobilePhoneNumber), typeof(RegisterResultEnum));
            registerHttpGet.FinishedGet += registerHttpGet_FinishedGet;
        }

        void registerHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return; //Eventueel foutmelding

                RegisterResultEnum result = (RegisterResultEnum)get.resultObject;

                switch (result)
                {
                    case RegisterResultEnum.Failure:
                        GlobalHelper.ShowMessage("Error", "Failed to register due to unknown error. \nPlease try again.");
                        break;
                    case RegisterResultEnum.Success:
                        GlobalHelper.ShowMessage("Information", "Your activation code has been sent to your phone.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/LoginPage.xaml");
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }
    }
}
